package edu.friday.service.impl;

import edu.friday.common.constant.UserConstants;
import edu.friday.model.SysMenu;
import edu.friday.model.vo.SysMenuVO;
import edu.friday.model.vo.SysUserVo;
import edu.friday.repository.SysMenuRepository;
import edu.friday.service.SysMenuService;
import edu.friday.utils.BeanUtils;
import edu.friday.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    SysMenuRepository sysMenuRepository;


    @Override
    public Set<String> selectMenuPermsByUserId(Long userId) {
        Set<String> permsSet = new HashSet<>();
        List<String> perms = sysMenuRepository.selectMenuPermsByUserId(userId);
        for (String perm : perms){
            if (StringUtils.isNotBlank(perm)){
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }

    @Override
    public List<SysMenu> selectMenuList(SysMenuVO sysMenuVO, Long userId) {
        List<SysMenu> sysMenuList = null;
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuVO, sysMenu);
        Sort sort=Sort.by("parentId","orderNum");
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withMatcher("menuName", ExampleMatcher.GenericPropertyMatchers.exact());
        Example example = Example.of(sysMenu, exampleMatcher);
        if (SysUserVo.isAdmin(userId)){
            sysMenuList = sysMenuRepository.findAll(example,sort);
        }
        else {
            sysMenuList = sysMenuRepository.selectMenuListByUserId(sysMenu,userId);
        }

        return sysMenuList;
    }

    @Override
    public String checkMenuNameUnique(SysMenu sysMenu) {
        Long menuId = sysMenu.getId();
        if (StringUtils.isNull(menuId)){
            menuId=-1L;
        }
        SysMenu menu = new SysMenu();
        menu.setMenuName(sysMenu.getMenuName());
        Page<SysMenu> list=sysMenuRepository.findAll(Example.of(menu), PageRequest.of(0, 1));

        SysMenu info = null;
        if (list.hasContent()){
            info = list.getContent().get(0);
        }
        if (StringUtils.isNotNull(info) && !info.getId().equals(menuId)){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int insertMenu(SysMenu sysMenu) {
        sysMenuRepository.save(sysMenu);
        if (StringUtils.isNull(sysMenu.getId())){
            return 0;
        }
        return 1;
    }

    @Override
    public int updateMenu(SysMenu menu) {
        Optional<SysMenu> sysMenuOptional=sysMenuRepository.findById(menu.getId());
        if (!sysMenuOptional.isPresent()){
            return 0;
        }
        SysMenu sysMenu=new SysMenu();
        BeanUtils.copyProperties(menu,sysMenu);
        sysMenuRepository.save(sysMenu);
        return 1;
    }

    @Override
    public boolean hasChildByMenuId(Long menuId) {
        SysMenu sysMenu=new SysMenu();
        sysMenu.setParentId(menuId);
        long result=sysMenuRepository.count(Example.of(sysMenu));
        return result > 0;
    }

    @Override
    public boolean checkMenuExistRole(Long menuId) {
        long result=sysMenuRepository.checkMenuExistRole(menuId);
        return result > 0;
    }

    @Override
    public int deleteMenuById(Long menuId) {
        sysMenuRepository.deleteById(menuId);
        return 1;
    }


}
